﻿/*
   Author: Tomer Paz
   games.com
   2007
*/
// The main Game object. initiate game board and start/shutdown the game
package monopoly.Game {
	import flash.display.Sprite;
	import flash.events.Event;
	import monopoly.GameController.GameController;
	import monopoly.IFS.GLOBALS;
	import monopoly.Game.GameArgs;
	import flash.display.Stage;
	import flash.display.StageAlign;
    import flash.display.StageScaleMode;
	
	public class Game extends Sprite
		{
		private var _ClientVersion:uint;
		private var _GameController:GameController;
		private var _RootSprite:Sprite;
		
		//constructor
		public function Game(ClientVersion:uint, RootSprite:Sprite) 
		    {
			_ClientVersion = ClientVersion;
			_RootSprite	= RootSprite;
						
			Init();
			}
		
		// initialize game
		private function Init():void
			{
			GLOBALS.ServerName = GameArgs.ServerName;
			GLOBALS.WebServerName = GameArgs.WebServer;
			SetupGUI();
			_GameController = new GameController(GameArgs.myIPAddrs, GameArgs.SecretKey, GameArgs.ClientID, _ClientVersion, GameArgs.GameID, _RootSprite);
			_GameController.addEventListener(Event.COMPLETE, onGameControllerLoad);
			_GameController.LoadDBInfo();
			}
			
		private function onGameControllerLoad(e:Event):void
			{
			addChild(_GameController);
			StartGame();
			}
			
		// minimal setup of GUI "skeleton". does not include fields content nor players. might include frames, sizes, background etc'
		private function SetupGUI():void
			{
			// load some pre-requisite information from web server using Load / URLLoad / AMFPHP (preferred)
			
			// start the damn drawing
			_RootSprite.stage.scaleMode = StageScaleMode.NO_SCALE;
			_RootSprite.stage.showDefaultContextMenu = false;
			drawRect();
			}
			
		// private functions - builders
				
		public function StartGame():void
			{
			// wait for user to enter the main Frame
			//this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			_GameController.Execute();
			}

		// react upon user entering main Frame
//		private function onEnterFrame(event:Event):void 
//			{
//			_GameController.HandleEnterFrame();
//			}	
		
		private function drawRect():void
			{
			this.graphics.beginFill( 0xffffff );
			//@TODO - take width+height from DB!
			this.graphics.drawRect( 0, 0, 800, 600 );
			this.graphics.endFill( );
			}
			
		}
}